﻿// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
    "use strict";
    var myLV;
    var inputArr = new WinJS.Binding.List();

    WinJS.UI.Pages.define("/pages/game/game.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {

            myLV = element.querySelector(".myLV").winControl;
            myLV.itemTemplate = element.querySelector(".igracTemplate");
            myLV.itemDataSource = inputArr.dataSource;
            myLV.selectionMode = WinJS.UI.SelectionMode.none;

            var rand;

            function GetSlika(randomItem) {
                element.querySelector('#playerImage').src = randomItem.slika;
                rand = randomItem;
            };

            function hideGame() {
                element.querySelector('.playerLabel').style.display = 'none';
                element.querySelector('.playerName').style.display = 'none';
                element.querySelector('.playerImage').style.display = 'none';
                element.querySelector('.playerSubmit').style.display = 'none';
                element.querySelector('.addButton').style.display = '-ms-grid';

                myLV.selectionMode = WinJS.UI.SelectionMode.multi;
            };

            function nameCheck() {
                var playerNameGuess = element.querySelector('.playerName').value;
                if (rand.ime == playerNameGuess) {
                    inputArr.push(rand);
                } else {
                    inputArr.splice(0, 1);
                }
            };

            function showGame() {
                element.querySelector('.playerLabel').style.display = '-ms-grid';
                element.querySelector('.playerName').style.display = '-ms-grid';
                element.querySelector('.playerImage').style.display = '-ms-grid';
                element.querySelector('.playerSubmit').style.display = '-ms-grid';
                element.querySelector('.addButton').style.display = 'none';
            };

            function addStickers() {
                

                WinJS.Navigation.navigate("/pages/home/home.html");
            };

            element.querySelector('.addButton').addEventListener("click", addStickers, false);
            
            var count = options.selectionCount;
            fullData.getRandomPicture(GetSlika);

            function checkAnswer() {
                count--;
                nameCheck();

                if (count == 0) {
                    hideGame();
                } else {
                    fullData.getRandomPicture(GetSlika);
                }
            }

            element.querySelector('.playerSubmit').addEventListener("click", checkAnswer, false);
        },

        unload: function () {
            // TODO: Respond to navigations away from this page.

            var indexes = myLV.selection.getIndices();
            var rest = filter(indexes, inputArr.length);

            indexes.forEach(function (index) {
                var item = inputArr.getAt(index);
                if (Data.checkDuplicate(item))
                    Data.igraciArr.push(item);
                else
                    Data.dodadiDuplikat(item);
            });

            rest.forEach(function (index) {
                Data.dodadiDuplikat(inputArr.getAt(index));
            });

            Data.saveToLocal();
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });

    function filter(arr, count) {
        var tmp = [];

        for (var i = 0; i < count; i++) {
            var flag = 1;
            arr.forEach(function (item) {
                if (item == i) flag = 0;
            });
            if (flag == 1) tmp.push(i);
        };

        return tmp;
    }

    var publicMembers = {
        inputArr: inputArr
    };

    WinJS.Namespace.define("gamePage", publicMembers);

})();
